#include <arpa/inet.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>

// 结构体用于保存客户端信息
struct ClientInfo {
    int socket;
    char username[100];
};

int main() {
    int server_socket, client_socket;
    struct sockaddr_in server_addr, client_addr;
    socklen_t client_addr_len = sizeof(client_addr);
    char buffer[1024];
    struct ClientInfo clients[2];  // 存储两个客户端信息的数组

    // 创建套接字
    server_socket = socket(AF_INET, SOCK_STREAM, 0);

    // 设置服务器地址结构
    server_addr.sin_family = AF_INET;
    server_addr.sin_addr.s_addr = INADDR_ANY;
    server_addr.sin_port = htons(8080);  // 选择一个合适的端口号

    // 将套接字绑定到地址和端口
    bind(server_socket, (struct sockaddr*)&server_addr, sizeof(server_addr));

    // 监听连接请求
    listen(server_socket, 2);  // 允许最多两个客户端连接

    printf("等待客户端连接...\n");

    /* // 接受两个客户端连接请求
    for (int i = 0; i < 2; i++) {
        clients[i].socket = accept(server_socket, (struct sockaddr*)&client_addr, &client_addr_len);
        printf("客户端 %d 连接成功！\n", i + 1);

        // 接收客户端用户名
        recv(clients[i].socket, clients[i].username, sizeof(clients[i].username), 0);
        printf("客户端 %d 用户名：%s\n", i + 1, clients[i].username);
    } */

    int connected_clients = 0;

    // 接受客户端连接并处理消息
    while (1) {
        if (connected_clients < 2) {
            clients[connected_clients].socket = accept(server_socket, (struct sockaddr*)&client_addr, &client_addr_len);
            printf("客户端 %d 连接成功！\n", connected_clients + 1);

            // 接收客户端用户名
            recv(clients[connected_clients].socket, clients[connected_clients].username, sizeof(clients[connected_clients].username), 0);
            printf("客户端 %d 用户名：%s\n", connected_clients + 1, clients[connected_clients].username);

            connected_clients++;
        }

        if (connected_clients == 2) {
            // 从客户端1接收消息并发送给客户端2
            recv(clients[0].socket, buffer, sizeof(buffer), 0);
            printf("%s 对 %s 说：%s\n", clients[0].username, clients[1].username, buffer);
            send(clients[1].socket, buffer, strlen(buffer), 0);

            // 从客户端2接收消息并发送给客户端1
            recv(clients[1].socket, buffer, sizeof(buffer), 0);
            printf("%s 对 %s 说：%s\n", clients[1].username, clients[0].username, buffer);
            send(clients[0].socket, buffer, strlen(buffer), 0);
        }

        /* // 发送和接收数据
        while (1) {
            // 从客户端1接收消息并发送给客户端2
            recv(clients[0].socket, buffer, sizeof(buffer), 0);
            printf("%s 对 %s 说：%s\n", clients[0].username, clients[1].username, buffer);
            send(clients[1].socket, buffer, strlen(buffer), 0);

            // 从客户端2接收消息并发送给客户端1
            recv(clients[1].socket, buffer, sizeof(buffer), 0);
            printf("%s 对 %s 说：%s\n", clients[1].username, clients[0].username, buffer);
            send(clients[0].socket, buffer, strlen(buffer), 0);
        } */

        // 关闭套接字
        close(server_socket);
        close(clients[0].socket);
        close(clients[1].socket);
        return 0;
    }